package org.cangku.controller;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.cangku.dao.SystemUserDao;
import org.cangku.dao.UserInfoDao;
import org.cangku.entity.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.SplittableRandom;

@Controller
@RequestMapping("/system")
public class PJSystemUserController {
    @Autowired
    private SystemUserDao systemUserDao;
    @Autowired
    private UserInfoDao userInfoDao;

    @RequestMapping("/mfQuery")
    @ResponseBody
    public Map<String,Object> query(String name,int roleid,int offset,int limit){
        Map<String,Object> map = new HashMap<String, Object>();

        map.put("rows",systemUserDao.getAllUser(name,roleid,offset,limit));
        map.put("total",systemUserDao.queryUserCount(name,roleid));

        return map;
    }

    /*添加用户信息并上传图片*/
    @RequestMapping(value = "addinfo" ,method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addStock(@RequestParam(value = "touxiang", required = false) MultipartFile[] multipartFile, HttpServletRequest request, String userName, String password,String remarks,int roleid) throws IOException {
        for (MultipartFile files : multipartFile) {
            //1.获取文件名字
            String originalFilename = files.getOriginalFilename();
            //2.图片的输入流名字
            try {
                InputStream inputStream = files.getInputStream();
                //3.存在于项目中的路径
                String wlPath = "F:\\ideaxiti\\CangKu\\src\\main\\webapp\\image/" + originalFilename;
                //4.逻辑路径，存到数据库中
                String ljPath = "../image/" + originalFilename;
                //5.临时路径
                String realPath = request.getSession().getServletContext().getRealPath("/");
                String lsPath = realPath + "//image//" + originalFilename;

                //6.如果没有那个文件夹就创建
                File wlFile = new File("F:\\ideaxiti\\CangKu\\src\\main\\webapp\\image");
                if (!wlFile.exists()) {
                    wlFile.mkdir();
                }
                File isFile = new File(realPath + "\\image\\");
                if (!isFile.exists()) {
                    isFile.mkdir();
                }
                //7.不为空时
                if (!files.isEmpty()) {
                    //输出流 写物理路径
                    FileOutputStream wlStream = new FileOutputStream(wlPath);
                    //输出流 写临时路径
                    FileOutputStream lsStream = new FileOutputStream(lsPath);
                    int len = 0;
                    while ((len = inputStream.read()) != -1) {
                        wlStream.write(len);//写入
                        lsStream.write(len);

                    }
                    wlStream.flush();
                    lsStream.flush();

                    wlStream.close();
                    lsStream.close();

                    inputStream.close();

                }
                System.out.println("存到文件夹位置："+wlPath);
                System.out.println("数据库字段："+ljPath);
                System.out.println("临时路径："+lsPath);
                Userinfo info = new Userinfo();
                info.setUserName(userName);
                /*把输入的密码加密*/
                String pwd = this.jiami(password);
                info.setPassword(pwd);
                info.setTouxiang(ljPath);
                info.setRemarks(remarks);
                info.setRoleid(roleid);

                userInfoDao.insert(info);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Map<String,Object> map = new HashMap<String, Object>();

        map.put("rows",systemUserDao.getAllUser("",0,0,2));
        map.put("total",systemUserDao.queryUserCount("",0));

        return map;
    }


    public static String jiami(String pwd){
        //所需加密的参数  即  密码
        String source = pwd ;
        //[盐] 一般为用户名 或 随机数
        String salt = "Shiro";
        //加密次数
        int hashIterations = 1024;
        SimpleHash sh = new SimpleHash("md5", source, salt, hashIterations);
        //打印最终结果
        return sh.toString() ;
    }

}
